package service;

import hibernate.IDao;
import hibernate.Page;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommonService implements ICommonService{
	
	@Autowired
	protected IDao dao;
	
	public Object findFirst(String hql, Object... args) {
		return dao.findFirst(hql, args);
	}

	public Integer findInt(String hql, Object... args) {
		return dao.findInt(hql, args);
	}
	
	public Long findLong(String hql, Object... args) {
		return dao.findLong(hql, args);
	}

	public String findString(String hql, Object... args) {
		return dao.findString(hql, args);
	}
	
	public <E> E findFirst(Class<E> clasz, String order, String cond, Object[] args, String... includes) {
		return dao.findFirst(clasz, order, cond,args,includes);
	}

	public <E> E findFirst(Class<E> clasz, String orderBy, Boolean isAsc, String sqlRestriction, String... includes) {
		return dao.findFirst(clasz, orderBy, isAsc, sqlRestriction, includes);
	}
	
	public List findAll(String hql, Object... args){
		return dao.findAll(hql, args);
	}
	
	public List findAll(Class clasz, String order, String cond,Object[] args, String... includes){
		return dao.findAll(clasz, order, cond, args, includes);
	}
	
	public <E> E getById(Class<E> clasz, Serializable id, String... includes) {
		return dao.getById(clasz, id, includes);
	}

	public <E> E getByProperty(Class<E> clasz, String name, Object value, String... includes) {
		return dao.getByProperty(clasz, name, value, includes);
	}

	/**
	 * save
	 */
	public void save(Object entity) {
		dao.save(entity);
	}

	public void saveOrUpdate(Object entity) {
		dao.saveOrUpdate(entity);
	}

	public void update(Object entity) {
		dao.update(entity);
	}


	public Page findPageBySQL(String sql, Object[] args, int start, int limit) {
		return dao.findPageBySQL(sql, args, start, limit);
	}
	
	public Page findPageByHQL(String hql, Object[] args, int start, int limit) {
		return dao.findPageByHQL(hql, args, start, limit);
	}
	public Page findPage(DetachedCriteria dc, int start, int limit) {
		return dao.findPage(dc, start, limit);
	}
	public Page findPage(DetachedCriteria dc, int start,int limit,String... includes){
		return dao.findPage(dc, start, limit,includes);
	}

	public void down(Class mappingClass, Integer id, String groupBy) {
		dao.down(mappingClass, id, groupBy);
		
	}

	public List findByCriteria(DetachedCriteria dc) {
		return dao.findByCriteria(dc);
	}

	public List findByCriteria(DetachedCriteria dc, int start, int limit) {
		return dao.findByCriteria(dc,start,limit);
	}

	public void up(Class mappingClass, Integer id, String groupBy) {
		dao.up(mappingClass, id, groupBy);
		
	}
	
	public void delete(Object entity) {
		dao.delete(entity);
	}

	public void delete(Class clazz,Object[] ids) {
		dao.delete(clazz,ids);
	}

	public void delete(Class clazz, Object id) {
		dao.delete(clazz,new Object[]{id});
	}

	public Object getMax(Class entityClass,String propertyName,String scope,Object value){
		return dao.getMax(entityClass, propertyName, scope, value);
	}
	public Object getMin(Class entityClass,String propertyName,String scope,Object value){
		return dao.getMin(entityClass, propertyName, scope, value);
	}

	public int executeUpdate(String executeHQL, Object... values) {
		return dao.executeUpdate(executeHQL, values);
	}

	public boolean isExist(Class<?> classname, String property, Object value, String whereClause, Object[] values) {
		return dao.isExist(classname,property,value,whereClause,values);
	}
}
